<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,100分)- 预订酒店（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>放暑假了&#xff0c;小明决定到某旅游景点游玩&#xff0c;他在网上搜索到了各种价位的酒店&#xff08;长度为n的数组A&#xff09;&#xff0c;他的心理价位是x元&#xff0c;请帮他筛选出k个最接近x元的酒店&#xff08;n&gt;&#61;k&gt;0&#xff09;,并<strong>由低到高</strong>打印酒店的价格。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行&#xff1a;n, k, x<br /> 第二行&#xff1a;A[0] A[1] A[2]…A[n-1]</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>从低到高打印筛选出的酒店价格</p> 
<p></p> 
<h4>备注</h4> 
<ol><li>酒店价格数组A和小明的心理价位x均为整型数据</li><li>优先选择价格最接近心理价位的酒店&#xff0c;若两家酒店距离心理价位差价相同&#xff0c;则选择价格较低的酒店。&#xff08;比如100元和300元距离心理价位200元同样接近&#xff0c;此时选择100元&#xff09;</li><li>酒店价格可能相同重复</li></ol> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">10 5 6<br /> 1 2 3 4 5 6 7 8 9 10</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">4 5 6 7 8</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:83px;">输入</td><td style="width:415px;"> <p>10 4 6<br /> 10 9 8 7 6 5 4 3 2 1</p> </td></tr><tr><td style="width:83px;">输出</td><td style="width:415px;">4 5 6 7</td></tr><tr><td style="width:83px;">说明</td><td style="width:415px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:81px;">输入</td><td style="width:417px;">6 3 1000<br /> 30 30 200 500 70 300</td></tr><tr><td style="width:81px;">输出</td><td style="width:417px;">200 300 500</td></tr><tr><td style="width:81px;">说明</td><td style="width:417px;">无</td></tr></tbody></table> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();
    int k &#61; sc.nextInt();
    int x &#61; sc.nextInt();

    int[] prices &#61; new int[n];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      prices[i] &#61; sc.nextInt();
    }

    System.out.println(getResult(n, k, x, prices));
  }

  /**
   * &#64;param n 长度为n的价位数组
   * &#64;param k 筛选出k个最接近x元的酒店
   * &#64;param x 心理价位是x元
   * &#64;param prices 价位数组
   * &#64;return 筛选出k个最接近x元的酒店&#xff08;n&gt;&#61;k&gt;0&#xff09;,并由低到高打印酒店的价格
   */
  public static String getResult(int n, int k, int x, int[] prices) {
    ArrayList&lt;int[]&gt; tmp &#61; new ArrayList&lt;&gt;();

    for (int p : prices) {
      tmp.add(new int[] {p, Math.abs(p - x)});
    }

    tmp.sort((a, b) -&gt; a[1] !&#61; b[1] ? a[1] - b[1] : a[0] - b[0]);

    StringJoiner sj &#61; new StringJoiner(&#34; &#34;);

    Integer[] ans &#61; tmp.stream().limit(k).map(arr -&gt; arr[0]).toArray(Integer[]::new);
    Arrays.sort(ans);

    for (Integer an : ans) {
      sj.add(an &#43; &#34;&#34;);
    }

    return sj.toString();
  }
}
</code></pre> 
<h4>JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const [n, k, x] &#61; lines[0].split(&#34; &#34;).map(Number);
    const prices &#61; lines[1].split(&#34; &#34;).map(Number);

    console.log(getResult(n, k, x, prices));

    lines.length &#61; 0;
  }
});

function getResult(n, k, x, prices) {
  const tmp &#61; [];

  for (let p of prices) {
    tmp.push([p, Math.abs(p - x)]);
  }

  tmp.sort((a, b) &#61;&gt; (a[1] !&#61; b[1] ? a[1] - b[1] : a[0] - b[0]));

  return tmp
    .slice(0, k)
    .map((arr) &#61;&gt; arr[0])
    .sort((a, b) &#61;&gt; a - b)
    .join(&#34; &#34;);
}
</code></pre> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n, k, x &#61; map(int, input().split())
prices &#61; list(map(int, input().split()))


# 算法入口
def getResult(n, k, x, prices):
    tmp &#61; []

    for p in prices:
        tmp.append((p, abs(p - x)))

    tmp.sort(key&#61;lambda x: (x[1], x[0]))

    ans &#61; list(map(lambda x: x[0], tmp[:k]))

    ans.sort()

    return &#34; &#34;.join(map(str, ans))


# 调用算法
print(getResult(n, k, x, prices))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>